import gdata.docs.data
import gdata.docs.client
import gdata.spreadsheet.service
import sys

c = gdata.spreadsheet.service.SpreadsheetsService()
client = gdata.docs.client.DocsClient()
client.ssl = True
c.email = 'science.uoit@gmail.com'
c.password = sys.argv[1]
c.ProgrammaticLogin()
client.ClientLogin(email='science.uoit@gmail.com',
                   password=sys.argv[1],
                   source="uoit-pages")

## List the folders

feed = client.GetDocList(uri="/feeds/default/private/full/-/folder")
for f in feed.entry:
  if f.title.text == 'pages - template - 0.1':
    print f.title.text
    key = f.id.text.split("/")[-1].split("%3A")[-1]
    uri = f.content.src
    break

## List the folder content
feed = client.GetDocList(uri=uri)
x = client.auth_token.token_string
client.auth_token.token_string = c.GetClientLoginToken()
for f in feed.entry:
  client.Export(f, "./%s.xls" % (f.title.text))
  print "downloaded", f.title.text

client.auth_token.token_string = x
for f in client.GetDocList(uri="/feeds/default/private/full/-/folder").entry:
  print f.title.text
